the_prefix <- ""

setwd(the_prefix)

# ### generate data subset from original data (not provided)
# load("joint-data-12072018.Rdata")
# dta18m <- subset(dta18m,select=c("mno",
#                                   "ACASCALE6","ACASCALE7",
#                                   "ACASCALE8",
#                                   "ACASCALE11",
#                                   "ACASCALE12","ACASCALE13",
#                                   "SELFINSURE10","COVERED10","MEDICAID10"))
# dim(dta18m)
#
# save(dta18m,
#      file="./data/figure2-panel-aca-replication-12112020.Rdata")
load(
    paste0(the_prefix, "/data/figure2-panel-aca-replication-12112020.Rdata")
    )

#### subset observations by insurance source in wave 10 (Jan. 2016)
flag <- ! dta18m$ACASCALE12 %in% c(NA) & ! dta18m$ACASCALE13 %in% c(NA)
sum(flag)
s.flag <- ! dta18m$ACASCALE12 %in% c(NA) & ! dta18m$ACASCALE13 %in% c(NA) &
  dta18m$SELFINSURE10 %in% c(1)
sum(s.flag)
m.flag <- ! dta18m$ACASCALE12 %in% c(NA) & ! dta18m$ACASCALE13 %in% c(NA) &
  dta18m$MEDICAID10 %in% c(1)
sum(m.flag)
u.flag <- ! dta18m$ACASCALE12 %in% c(NA) & ! dta18m$ACASCALE13 %in% c(NA) &
  dta18m$COVERED10 %in% c(0)
sum(u.flag)

#### create vector
aca.scale.all <- c(
  mean(dta18m$ACASCALE6[flag],na.rm=T),
  mean(dta18m$ACASCALE7[flag],na.rm=T),
  mean(dta18m$ACASCALE8[flag],na.rm=T),
  mean(dta18m$ACASCALE11[flag],na.rm=T),
  mean(dta18m$ACASCALE12[flag],na.rm=T),
  mean(dta18m$ACASCALE13[flag],na.rm=T)
)

aca.scale.s <- c(
  mean(dta18m$ACASCALE6[s.flag],na.rm=T),
  mean(dta18m$ACASCALE7[s.flag],na.rm=T),
  mean(dta18m$ACASCALE8[s.flag],na.rm=T),
  mean(dta18m$ACASCALE11[s.flag],na.rm=T),
  mean(dta18m$ACASCALE12[s.flag],na.rm=T),
  mean(dta18m$ACASCALE13[s.flag],na.rm=T)
)

aca.scale.u <- c(
  mean(dta18m$ACASCALE6[u.flag],na.rm=T),
  mean(dta18m$ACASCALE7[u.flag],na.rm=T),
  mean(dta18m$ACASCALE8[u.flag],na.rm=T),
  mean(dta18m$ACASCALE11[u.flag],na.rm=T),
  mean(dta18m$ACASCALE12[u.flag],na.rm=T),
  mean(dta18m$ACASCALE13[u.flag],na.rm=T)
)

pdf(
    paste0(the_prefix, "/figs/figure2_aca-scale-over-time-20190222-byinsurance.pdf"),
    width=5, height=4.5
)
par(mar=c(5,5,0,1))
plot(aca.scale.all,x=as.Date(c("2012-10-01","2012-12-01","2014-10-01","2016-10-01","2016-12-01","2018-10-25")),
     type="b",ylim=c(1,9),xaxt="n",pch=16,xlab="Date",ylab="ACA Attitudes",cex.lab=1.6,
     xlim=as.Date(c("2012-01-01","2018-12-01")),yaxt="n",bty="n",lwd=4, cex=1.3)
axis(side=2,at=1:7,labels=c("Keep ACA","","","","","",""), cex.axis=1.3)
axis(side=2,at=1:7,labels=c("","","3","4","5","",""), cex.axis=1.3)
axis(side=2,at=7,labels=c("Repeal ACA"), cex.axis=1.4)

axis(side=1,at=as.Date(c("2012-01-01","2013-01-01","2014-01-01","2015-01-01","2016-01-01","2017-01-01","2018-01-01","2019-01-01")),labels=c("2012","2013","2014","2015","2016","2017","2018","2019"), cex.axis=1.25)
par(new=T)
plot(aca.scale.s,x=as.Date(c("2012-10-01","2012-12-01","2014-10-01","2016-10-01","2016-12-01","2018-10-25")),
      type="b",ylim=c(1,9),xaxt="n",pch=17,col="darkgray",yaxt="n",main="",xlab="",ylab="",xlim=as.Date(c("2012-01-01","2018-12-01")),bty="n",lwd=4, cex=1.3)
par(new=T)
plot(aca.scale.u,x=as.Date(c("2012-10-01","2012-12-01","2014-10-01","2016-10-01","2016-12-01","2018-10-25")),type="b",ylim=c(1,9),xaxt="n",pch=18,
      col="gray",yaxt="n",main="",xlab="",ylab="",xlim=as.Date(c("2012-01-01","2018-12-01")),bty="n",lwd=4, cex=1.5)
text(x=as.Date("2017-10-01"),y=aca.scale.u[3]+0.5,"Not Covered",cex=1.4,col=gray(0.5))
text(x=as.Date("2017-10-01"),y=aca.scale.all[3]+.1,"All",cex=1.4,col="black")
text(x=as.Date("2017-06-01"),y=aca.scale.s[3]-0.6,"Self-Purchased",cex=1.4,col=gray(0.5))
#text(x=as.Date("2018-11-01"),y=aca.scale.all[5]+.4,"?",cex=2,col="black")
#abline(v=as.Date("2014-01-01"))

lines(x=c(as.Date("2014-01-01"),as.Date("2014-01-01")),
      y=c(0,6.25),col="black",lwd=1.5,lty=2)
text("Exchanges \n Open",x=as.Date("2014-01-01"),y=7,cex=1.4)

mtext(side=3, text="Based on 2016\ninsurance sources",at=as.Date("2016-10-01"),padj=2.5, cex=1.2)
arrows(
    x0=as.Date("2016-10-01"),
    x1=as.Date("2016-10-01"),
    y0=6.9,
    y1=6.5,
    length=0.05
)

jitter <- rnorm(length(na.omit(dta18m$ACASCALE6[flag])),mean=0,sd=.1)
for(i in 1:length(na.omit(dta18m$ACASCALE6[flag]))){
  lines(x=c(as.Date("2012-02-01"),as.Date("2012-03-01")),y=c(na.omit(dta18m$ACASCALE6)[i]+jitter[i],na.omit(dta18m$ACASCALE6)[i]+jitter[i]),col="gray",lwd=.5)
}

dev.off()
